package com.bw.mvvm_common.mvvm.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.model.BaseModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/12 0012 下午 2:25
 *
 */
abstract class BaseViewModel <M extends BaseModel> extends ViewModel implements IVeiwModel, Observer<BaseEntity> {

    protected M m;
    protected CompositeDisposable compositeDisposable;
    private UIChange uiChange;

    @Override
    public void onCreate() {
        //初始化LiveData封装
        compositeDisposable = new CompositeDisposable();
        initModel();
    }

    @Override
    public void onDestory() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    //反射获取范型class类型->创建对象
    private void initModel(){
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[]types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[0];
        try {
            m = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }


    protected abstract void result(BaseEntity entity);

    @Override
    public void onSubscribe(  Disposable d) {
        compositeDisposable.add(d);
        showDialog();
    }

    @Override
    public void onNext(  BaseEntity baseEntity) {

        result(baseEntity);
    }

    @Override
    public void onError(  Throwable e) {
        showMsg("请求出错:"+e.getMessage());
    }

    @Override
    public void onComplete() {
        hideDialog();
    }

    //提供到View层进行LiveData数据绑定
    //数据驱动View层对应观察者
    //观察者下调用对应封装的显示对话框等方法
    public UIChange getUiChange() {
        if (uiChange == null)
            uiChange = new UIChange();
        return uiChange;
    }

    protected void showDialog(){
        getUiChange().setShowDialogObservable(""+System.currentTimeMillis());
    }

    protected void hideDialog(){
        getUiChange().setHideDialogObservable(""+System.currentTimeMillis());
    }

    protected void finishPage(){
        getUiChange().setFinishActObservable(""+System.currentTimeMillis());
    }

    protected void showMsg(String msg){
        getUiChange().setShowMsgObservable(msg);
    }

    protected void startPage(HashMap<String,Object>map){
        getUiChange().setStartPageObservable(map);
    }

    public class UIChange{
        private MutableLiveData<String>showDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String>hideDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String>showMsgObservable = new MutableLiveData<>();
        private MutableLiveData<String>finishActObservable = new MutableLiveData<>();
        private MutableLiveData<HashMap<String,Object>>startPageObservable = new MutableLiveData<>();

        public void setFinishActObservable(String finishActObservable) {
            this.finishActObservable.setValue(finishActObservable);
        }

        public void setHideDialogObservable(String hideDialogObservable) {
            this.hideDialogObservable.setValue(hideDialogObservable);
        }

        public void setShowDialogObservable(String showDialogObservable) {
            this.showDialogObservable.setValue(showDialogObservable);
        }

        public void setShowMsgObservable(String showMsgObservable) {
            this.showMsgObservable.setValue(showMsgObservable);
        }

        public void setStartPageObservable(HashMap<String, Object> startPageObservable) {
            this.startPageObservable.setValue(startPageObservable);
        }



        public MutableLiveData<HashMap<String, Object>> getStartPageObservable() {
            return startPageObservable;
        }

        public MutableLiveData<String> getFinishActObservable() {
            return finishActObservable;
        }

        public MutableLiveData<String> getHideDialogObservable() {
            return hideDialogObservable;
        }

        public MutableLiveData<String> getShowDialogObservable() {
            return showDialogObservable;
        }

        public MutableLiveData<String> getShowMsgObservable() {
            return showMsgObservable;
        }
    }
}
